#Persistent

VarSetCapacity(IdleTicks,8,0)

SetTimer, CheckCPULoad, 1000
return

CheckCPULoad:
  l := GetCPULoad()
  CPU_Load_Display(l)

  return

GetCPULoad()
{
  Global
  SetBatchLines, -1
  IdleTime0 = %IdleTime%  ; Save previous values
  Tick0 = %Tick%
  DllCall("kernel32.dll\GetSystemTimes", "uint",&IdleTicks, "uint",0, "uint",0)
  IdleTime := *(&IdleTicks)
  Loop 7                  ; Ticks when Windows was idle
    IdleTime += *( &IdleTicks + A_Index ) << ( 8 * A_Index )
  Tick := A_TickCount     ; Ticks all together
  load := Abs(100 - 0.01*(IdleTime - IdleTime0)/(Tick - Tick0)/8)
  Return, load
}

CPU_Load_Display(load)
{
  Global
        CPU_Load_Display_Count := Floor(load/10)*2
        if (CPU_Load_Display_Count > 0) {
                SetTimer, CPU_Load_NL_Toggle, 50
        }
        return

        CPU_Load_NL_Toggle:
        CPU_Load_Display_Count--
        CPU_Load_NL_flg := !CPU_Load_NL_flg
        If (CPU_Load_NL_flg) {
                SetNumLockState, On
        } Else {
                SetNumLockState, Off
        }

        If (CPU_Load_Display_Count <= 0) {
                SetTimer, CPU_Load_NL_Toggle, Off
        }
        return
}

